url = 'https://www.acwing.com/problem/content/3551/'

n = int(input())

data = {}
l = 0
r = -1

for i in range(n):
    o, num = input().split(" ")
    num = int(num)
    if o == 'L':
        data[num] = l
        l += 1
    elif o == 'R':
        data[num] = r
        r -= 1
    else:
        if data[num] == l or data[num] == r:
            print(0)
        else:
            index = data[num]
            print(min(l - index, abs(index - r)) - 1)
